<?php
global $_W,$_GPC;
load()->func('tpl');
  
function is_weixin(){

	if ( strpos($_SERVER['HTTP_USER_AGENT'],

			'MicroMessenger') !== false ) {

			return true;

	}

	return false;

}

if(is_weixin()){
	
}else{
	
	$_W["openid"]='oU4go0msnyP2JizVuKdoNoZ1xLqo';
}
   
  
	$openid = $_W["openid"];
	$rid=$_GPC["rid"];
	$fromid=$_GPC["fromid"];//分享人openid
	
	$reply=pdo_fetch("SELECT * FROM " . tablename('dt_question_reply') . " WHERE rid = :rid ", array(':rid' => $rid));
 	$total=$reply["questioncount"];//问题数量
 	$dtuser = pdo_fetch("SELECT * FROM ". tablename('dt_userpoints')." WHERE openid=:openid and rid=:rid ORDER BY ID DESC LIMIT 1" , array(":openid"=>$openid,":rid"=>$rid));
	$huodongname=$reply["huodongname"];
 	$color=$reply["color"];
 	$iswanshan=$reply["iswanshan"];
	
	$from_user = ! empty ( $_W ['openid'] ) ? $_W ['openid'] : $_W ['fans'] ['from_user'];
	
	$account = WeAccount::create ( $_W ['account'] );
	$userinfo = $account->fansQueryInfo ( $from_user );
	
	
	$show_qrcode=0;
	if($reply["subscribe"]==1 && $userinfo["subscribe"]!=1){
		$show_qrcode=1;
	}
	
 	$userinfo_2=pdo_fetch("select * from ".tablename("dt_userinfo")." where openid=:openid and rid = :rid",array(":openid"=>$openid,":rid"=>$rid));
	if(!empty($userinfo_2["name"])){
		$iswanshan=0;
	}
	
	$dtuser_count=pdo_fetchcolumn("select count(*) from ims_dt_userpoints where rid=$rid and openid='$openid' and FROM_UNIXTIME(unix_timestamp(time),'%Y-%m-%d')=FROM_UNIXTIME(unix_timestamp(now()),'%Y-%m-%d')");
	//可答题
	if($userinfo_2["shyucount"]-$dtuser_count > 0){
		//pdo_update("dt_userinfo",array("shyucount"=>$userinfo_2["shyucount"]-1),array("openid"=>$openid,"rid"=>$rid));
	
		$sql="SELECT * FROM " . tablename('dt_question_reply') . " WHERE rid = :rid LIMIT 1";
		$quesreply = pdo_fetch($sql, array(':rid' => $rid));
		
		$huodongname=$quesreply["huodongname"];
		$followdesc=$quesreply["followdesc"];
		$huodongdesc=$quesreply["huodongdesc"];
		
		if ($quesreply['start_time'] > time()) {
			//倒计时
			$startsecond=$quesreply["start_time"];
			$now = time();
			$leftsecond=$startsecond-$now;
			include $this->template('nostart');
			exit();
		}
		
		if ($quesreply == false || $quesreply['start_time'] > time() || $quesreply['end_time'] < time() || $quesreply['status']==0) {
			include $this->template('over');
			exit();
		}
		
		
		//企业信息
// 		$qyname=$quesreply["qyname"];
// 		$qylogo=$quesreply["qylogo"];
// 		$qylink=$quesreply["qylink"];
// 		$homebg=$quesreply["homebg"];
		
		//答题记录
		$result=pdo_fetch("select count(*) as num from ".tablename("dt_userpoints")." where rid=:rid",array(":rid"=>$rid));
		$num = (int)$result['num'];
		
		
// 		$isshareadd=$reply["isshareadd"];//是否 分享增加一次
		
// 		//如果超过分享次数上限
// 		$sharelist=pdo_fetchall("select * from ".tablename("dt_usershare")." where fromuserid=:fromid and rid=:rid",array(":fromid"=>$openid,":rid"=>$rid));
// 		$is_max=0;
// 		if($reply["maxcount"]!=0 && !empty($reply["maxcount"]) && $reply["maxcount"]<=count($sharelist)){
// 			$is_max=1;
// 		}
		
// 		$count=$num+(int)$reply["xncount"];
		
		/**
		 * 分享
		 */
		
		$sharetitle=$reply["sharetitle"];//分享标题
		$sharecontent=$reply["sharecontent"];//分享内容
		$shareimg=$_W['attachurl'].$reply["shareimg"];//分享图片
		$isshareadd=$reply["isshareadd"];//是否 分享增加一次
		$sharelink="";
		$huodongname=$reply["huodongname"];
		$color=$reply["color"];
		if($isshareadd==0){
			$sharelink = $_W ['siteroot'] . 'app/' . $this->createMobileUrl ( 'index', array ('rid' => $rid));
		}else{
			$sharelink=$_W ['siteroot'] . 'app/' . $this->createMobileUrl ( 'index', array ('rid' => $rid,'fromid'=>$openid));
		}
		$sharelink = str_replace ( './', '', $sharelink );//分享链接
	}
	
	//超过每天频率
 	if($userinfo_2["shyucount"]-$dtuser_count <= 0){
 		
 		message('您今天的答题次数已用完请明天再参加！', referer(), 'success');
 		$sql="SELECT * FROM " . tablename('dt_question_reply') . " WHERE rid = :rid LIMIT 1";
		$shyucount=0;
 		$quesreply = pdo_fetch($sql, array(':rid' => $rid));
 		$huodongname=$quesreply["huodongname"];
 		$followdesc=$quesreply["followdesc"];
 		$huodongdesc=$quesreply["huodongdesc"];
 		//企业信息
 		$qyname=$quesreply["qyname"];
 		$qylogo=$quesreply["qylogo"];
 		$qylink=$quesreply["qylink"];
 		$homebg=$quesreply["homebg"];
 		include $this->template('index');
 	}else{
 		
		$sql="SELECT qrcode FROM " . tablename('dt_question_reply') . " WHERE rid = :rid LIMIT 1";
 		$qrcode = pdo_fetch($sql, array(':rid' => $rid));
		$questime=$reply["questiontime"];//答题总时间   已改成每道题时间
		
		
		//$questtimes=intval($questime)*60;
		$questtimes=intval($questime);
		$isrand=$reply["isrand"];//是否随机
		
		//if($isrand==1){
			//从该活动的所有问题中随机抽出$total条数据
			$questionlist = pdo_fetchall("SELECT * FROM ". tablename('dt_question') . " WHERE rid = :rid and is_must=:is_must ORDER BY RAND() LIMIT ".intval($total-1), array(':rid' => intval($rid),':is_must' => 0));
		//}else{
			//从该活动的所有问题中抽出前$total条数据
			//$questionlist = pdo_fetchall("SELECT * FROM " . tablename('dt_question') . " WHERE rid=:rid and is_must=:is_must ORDER BY id LIMIT ".intval($total-1), array(':rid' => $rid,':is_must' => 0));
		//}
		
		//取必选题
		if($userinfo_2['mustdate']==date('Y-m-d',time())){
			  $mustid=$userinfo_2['mustid'];
			$mustquest=pdo_fetch("select * from ims_dt_question where id=$mustid");
		}else{
			$mustquest=pdo_fetch("select * from ims_dt_question where is_must=1 ORDER BY RAND()");
		}
			
		
		$questionlist[]=$mustquest;
		
		for($i=0;$i<count($questionlist);$i++){
			$quest=$questionlist[$i];
			//答案
			$realanswers.=$quest["realanswer"].",";
		}
		//总分   已改成宝藏
		$zongfen=intval($reply["zongfen"]);
		$onepoints=$zongfen/intval($total);//每题分值
		setcookie("stime", time(), time()+3600);
		include $this->template('dati');
 	}